package dao.impl;

import dao.CategoryDao;
import pojo.Category;
import util.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tiler on 2020/11/4
 */
public class CategoryDaoImpl implements CategoryDao {
    @Override
    public List<Category> selectAllCategories() {
        List<Category> categories = new ArrayList<>();

        String sql = "select * from category";

        JDBCUtil jdbcUtil = new JDBCUtil();

        ResultSet resultSet = jdbcUtil.excuteQuery(sql);

        try {
            while(resultSet.next()) {
                Category category = new Category();

                category.setCategoryId(resultSet.getInt("categoryId"));
                category.setCategoryName(resultSet.getString("categoryName"));

                categories.add(category);
            }

            return categories;
        } catch (SQLException e) {
            e.printStackTrace();

            return null;
        }
        finally {
            jdbcUtil.close();
        }
    }
}
